Components of the time series
When analyzing a time series, three components are distinguished: trend, seasonality and noise. A trend is a general trend, seasonality, as the name implies, the effects of periodicity (day of the week, time of year, etc.) and, finally, noise are random factors.
In order to understand the difference between these three quantities, we will simulate the function of the distance from the earth to the moon. It is known that on average the moon every year it moves away by 4 cm - this is a trend, during the day the moon rotates around the earth and the distance fluctuates from ~362600 km to ~405400 km is seasonality. Noise is "random" factors, for example, the influence of other planets. If we draw the sum of these three graphs, then we get a time series - a function showing the change in distance from the earth to the moon in time.
Trend. Smoothing methods
Smoothing methods are necessary to remove noise from the time series. There are various ways of smoothing, the main ones are the moving average method and the exponential smoothing method.
Moving average method
The idea of the moving average method is to shift the chart point by the average value of a certain interval. An odd number of sections are taken as an interval, for example, three - the previous, current and next periods, the average is found and is taken as a smoothed value:
Si = Σkj=-k(xi+j)/(2k+1)
This method has a problem: a random high or low value strongly affects the moving line. As weights were introduced to the solutions. Window functions are used for weight distribution, the main window functions are the Dirichlet window (rectangular function), B-splines, polynomials, sinusoidal and cosine:
The disadvantages of using a moving average are the complexity of calculations and incorrect data at the ends of the chart.
Source data | Moving Average | Weighted moving average (sinusoidal window, n=5) | Weighted moving average (Gann window, n=5) |
800 | 854 | 283 | 0 |
908 | 845 | 481 | 400 |
828 | 902 | 515 | 454 |
1070 | 1032 | 557 | 414 |
1320 | 1141 | 647 | 535 |
1345 | 1198 | 757 | 660 |
1058 | 1325 | 757 | 673 |
1577 | 1567 | 781 | 529 |
2288 | 1611 | 986 | 789 |
1520 | 1889 | 1119 | 1144 |
2170 | 1736 | 1168 | 760 |
965 | 1613 | 982 | 1085 |
1796 | 1768 | 942 | 483 |
2139 | 2034 | 998 | 898 |
3236 | 2454 | 1424 | 1070 |
2645 | 2269 | 1655 | 1618 |
1056 | 1934 | 1420 | 1323 |
800 | 1969 | 873 | 528 |
3374 | 2348 | 983 | 400 |
4163 | 2779 | 2294 | 1687 |
Table 1. Moving average smoothing |
As can be seen from the graph, an increase in n gives a smoother function, thus leveling out smaller fluctuations in the time series. Note that when smoothing, it does not matter whether the average graph matches the data graph or not, the goal is to build the correct form.
Exponential smoothing method
The exponential smoothing method got its name because the smoothed function exponentially decreases the influence of the previous period with a certain sensitivity coefficient. The smoothed value is found as the difference between the previous valid value and calculated value:
D't = α·Dt-1 + (1-α)·D't-1
The sensitivity coefficient, α, is selected between 0 and 1, the value 0.3 is used as the basis. If there is a sufficient sample, then the coefficient is selected by optimization.
Source data | Exponential smoothing, α=0.1 | Exponential smoothing, α=0.6 | |
800 | 800 | 800 | |
908 | -640 | 160 | |
828 | 667 | 481 | |
1070 | -518 | 304 | |
1320 | 573 | 520 | |
1345 | -384 | 584 | |
1058 | 480 | 573 | |
1577 | -326 | 406 | |
2288 | 451 | 784 | |
1520 | -177 | 1059 | |
2170 | 311 | 488 | |
965 | -63 | 1107 | |
1796 | 153 | 136 | |
2139 | 42 | 1023 | |
3236 | 176 | 874 | |
2645 | 165 | 1592 | |
1056 | 116 | 950 | |
800 | 1 | 254 | |
3374 | 79 | 378 | |
4163 | 266 | 1873 | |
Table 2. Exponential smoothing |
Forecasting methods
Forecasting methods are based on identifying trends in a time series and then using the found value to predict future values. In forecasting methods, trend and seasonality are distinguished, in general, all types of seasonality can be found by successive iterations. For example, when analyzing data for a year, you can highlight the seasonality of the time of year, and in the remaining the trend is to find seasonality by days of the week and so on.
Double exponential smoothing
Double exponential smoothing produces a smoothed level and trend value.
Attention! There may be confusion, the Holt-Winters method differs in terms: trend, seasonality and noise the level, trend and seasonality are called respectively.
Smooth - smoothing, smoothed level for the period τ, sτ, depends on the value of the level for the current period (Dτ), the trend for the previous period (tτ-1) and the calculated smoothed value for the previous period (sτ-1):
sτ = αDτ + (1 - α)(sτ-1 + tτ-1)
Trend - trend, trend for the period τ, tτ, depends on the calculated smoothed value for the previous and current periods (sτ and sτ-1) and from the previous trend:
tτ = β(sτ-sτ-1) + (1-β)tτ-1
The level and trend calculated according to these formulas can be used in forecasting:
D'τ+h = sτ + h·tτ
When calculating, the values of s and t for the first period are assigned s1 = D1 and t=0
Holt-Winters method
The Holt-Winters method includes a seasonal component, i.e. periodicity. There are two varieties of the method - multiplicative and additive. Unlike double exponential smoothing, the Holt-Winters method also studies the influence of periodicity.
The general idea of finding the values of the smoothed level, trend and periodicity is as follows: the smoothed level (s - smooth, sometimes l - level is used) is the base level of values, the trend (t - trend) is an indicator of the growth rate, the difference between the smoothed values of the current and previous period. To study the periodicity (p - period), we divide the data into periods of size k and highlight the influence of each element (1,2,...,k) of the period on smoothed level.
For more accurate calculations, a feedback indicator is introduced.
In a general sense, feedback is the influence of previous values for new ones: for example, when you start talking, you adjust the volume of your voice depending on what your ears hear - this is feedback.
To start the calculations, the values of s, t and k, in the simplest form, can be selected as sτ = Dτ, t = 0, p = 0.
k is the length of the selected period:
sτ = α(Dτ - pτ-k) + (1 - α)(sτ-1 + tτ-1)
Adjusted for previous values of tτ-k (feedback)
tτ = β(sτ-sτ-1) + (1-β)tτ-1
Adjusted for previous values of pτ-k (feedback)
pτ = γ(Dτ - sτ) + (1-γ)pτ-k
The following formula is used for forecasting:
xτ+h = Dτ + htτ + pτ-k+h
Holt-Winters multiplicative method
The multiplicative method differs from the additive one in that the parameters affecting the periodicity and the smoothed level calculated by the ratio:
pτ = γ(Dτ/sτ) + (1-γ)pτ-k
sτ = α(Dτ/pτ-k) + (1 - α)(sτ-1 + tτ-1)
tτ = β(sτ-sτ-1) + (1-β)tτ-1
The following formula is used for forecasting:
xτ+h = (Dτ + htτ)pτ-k+h
Holt-Winters method in excel
Table for downloading in formats ods and xls.
Prediction quality
Checking the quality of forecasting is possible if there is a sufficient sample and is an important test for reliability to check and optimize the values of α, β and γ, it is necessary to build a forecast on existing data, for example, if we have data for five years and we want to predict the next year, then it is necessary to build a model on the first four years, check and optimize the coefficients to minimize the error between forecast and data for the 5th year. After optimization the model can be rebuilt taking into account the last period to improve accuracy, followed by the construction of a forecast.
Optimization methods will be described in a separate article, below is an example of forecasting by the Holt Winters method.
# | Data | s | t | p | s | t | p |
---|---|---|---|---|---|---|---|
1 | 93 | 93 | 0 | 0 | 93 | 0 | 0 |
2 | 91 | 92 | -0.1 | -0.5 | 92 | -0.1 | 0.99 |
3 | 72 | 84 | -0.89 | -6 | 84 | -0.89 | 0.93 |
4 | 75 | 80 | -1.2 | -2.5 | 80 | -1.2 | 0.97 |
5 | 75 | 77 | -1.38 | -1 | 77 | -1.38 | 0.99 |
6 | 57 | 68 | -2.14 | -5.5 | 68 | -2.14 | 0.92 |
7 | 66 | 66 | -2.13 | 0 | 66 | -2.13 | 1 |
8 | 123 | 88 | 0.28 | 17.5 | 38 | -4.72 | 1.62 |
9 | 85 | 87 | 0.15 | -1.25 | 54 | -2.65 | 1.28 |
10 | 85 | 89 | 0.34 | -5 | 67 | -1.09 | 1.1 |
11 | 91 | 91 | 0.51 | -1.25 | 77 | 0.02 | 1.08 |
12 | 102 | 96 | 0.96 | 2.5 | 87 | 1.02 | 1.08 |
13 | 73 | 90 | 0.26 | -11.25 | 85 | 0.72 | 0.89 |
14 | 60 | 78 | -0.97 | -9 | 75 | -0.35 | 0.9 |
15 | 99 | 79 | -0.77 | 18.75 | 69 | -0.92 | 1.53 |
16 | 108 | 91 | 0.51 | 7.88 | 75 | -0.23 | 1.36 |
17 | 98 | 96 | 0.96 | -1.5 | 80 | 0.29 | 1.16 |
18 | 104 | 100 | 1.26 | 1.38 | 87 | 0.96 | 1.14 |
19 | 83 | 93 | 0.43 | -3.75 | 84 | 0.56 | 1.03 |
20 | 68 | 88 | -0.11 | -15.63 | 81 | 0.2 | 0.86 |
21 | 62 | 81 | -0.8 | -14 | 76 | -0.32 | 0.86 |
22 | 59 | 64 | -2.42 | 6.88 | 61 | -1.79 | 1.25 |
23 | 80 | 66 | -1.98 | 10.94 | 59 | -1.81 | 1.36 |
24 | 121 | 87 | 0.32 | 16.25 | 76 | 0.07 | 1.38 |
25 | 112 | 97 | 1.29 | 8.19 | 85 | 0.96 | 1.23 |
26 | 85 | 94 | 0.86 | -6.38 | 85 | 0.86 | 1.02 |
27 | 106 | 106 | 1.97 | -7.82 | 101 | 2.37 | 0.95 |
28 | 82 | 103 | 1.47 | -17.5 | 100 | 2.03 | 0.84 |